package com.cloud.template.netty.server;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;

/**
 * 服务端处理器，适用于netty5
 * Created by Viole on 2018/1/2.
 */
public class ServerHandler extends ChannelHandlerAdapter {

    //接收结果
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception{
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("client:"+buf.toString(CharsetUtil.UTF_8));
    }

    //刷新
    public void channelReadComplete(ChannelHandlerContext ctx){
        ctx.flush();
    }

    //关闭
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
